package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.CustomerService;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.CustomerServiceMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class CustomerServiceFroAct {
    /**
     * 店铺在线客服列表页
    */
    public static final String TPL_CUSTOMER_SERVICE_LIST = "tpl.customerServiceList";
   /**
     * 店铺在线客服添加页
     */
    public static final String TPL_CUSTOMER_SERVICE_ADD = "tpl.customerServiceAdd";
    /**
     * 店铺在线客服修改页
     */
    public static final String TPL_CUSTOMER_SERVICE_EDIT = "tpl.customerServiceEdit";
	
	
    //在线客服列表
	@RequestMapping(value = "/customerService/list*.jspx")
	public String list(String cl,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("cl", cl);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_CUSTOMER_SERVICE_LIST), request);
	}
	
    //在线客服添加
	@RequestMapping(value = "/customerService/add.jspx")
	public String add(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_CUSTOMER_SERVICE_ADD), request);
	}
	
    //在线客服保存
	@RequestMapping(value = "/customerService/save.jspx" ,method = RequestMethod.POST)
	public String save(CustomerService bean,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		bean.setWebsite(web);
		bean.setStore(store);
		manager.save(bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_CUSTOMER_SERVICE_ADD), request);
	}
	
    //在线客服修改
	@RequestMapping(value = "/customerService/edit.jspx")
	public String edit(Long id,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		CustomerService bean=manager.findById(id);
		model.addAttribute("bean", bean);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,TPL_CUSTOMER_SERVICE_EDIT), request);
	}
	
    //在线客服更新
	@RequestMapping(value = "/customerService/update.jspx",method = RequestMethod.POST)
	public String update(CustomerService bean,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		if(member==null){
			return "redirect:../login.jspx";
		}
		manager.update(bean);
		return "redirect:list.jspx";
	}
	
	//在线客服排序
	@RequestMapping("/customerService/priority.jspx")
	public String priority(Long[] wids, Integer[] priority, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		manager.updatePriority(wids, priority);
		model.addAttribute("message", "global.success");
//		return list(pageNo, request, model);
		return "redirect:list*.jspx";
	}
	
	//在线客服删除
	@RequestMapping("/customerService/delete.jspx")
	public String qqDelete(Long[] ids, Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		manager.deleteByIds(ids);
//		return list(pageNo, request, model);
		return "redirect:list*.jspx";
	}
	
	@Autowired
	private CustomerServiceMng manager;

}
